<template>
  <div>
    <br />
    <a-card class="card-content">
      <a-table
        :scroll="{ x: 1500 }"
        :columns="columns"
        :data-source="tableData"
        :loading="loading"
        childrenColumnName="children"
        :pagination="pagination"
        @change="handleTableChange"
        defaultExpandAllRows
        bordered
        :rowKey="(record, index) => record.id"
      >
        <template slot="productWork" slot-scope="text, record">
          <div
            v-for="(item, worKIndex) in record.productWorkStageList"
            :key="worKIndex"
          >
            {{ item.workStageId }} {{ item.workStageName }} 工时:{{
              item.processTime
            }}
          </div>
        </template>
        <template slot="status" slot-scope="text, record">
          <a-popconfirm
            okText="确定"
            cancelText="取消"
            v-if="tableData.length"
            title="确定修改状态吗?"
            @confirm="() => handleStatus(record)"
          >
            <a-switch
              :checked="record.status == 0 ? false : true"
              checked-children="已完成"
              un-checked-children="未完成"
              default-checked
            />
          </a-popconfirm>
        </template>
        <template slot="action" slot-scope="text, record">
          <a @click.stop="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click.stop="handleWork(record)">工序</a>
          <a-divider type="vertical" />
          <a @click.stop="handleWorkOrder(record)">工单详情</a>
          <a-divider type="vertical" v-if="record.automaticScheduling === 0" />
          <a
            @click.stop="handleWorkAutoMatic(record)"
            v-if="record.automaticScheduling === 0"
            >自动排单</a
          >
          <a-divider type="vertical" />
          <a-icon
            type="qrcode"
            @click.stop="handleCode(record)"
            style="fontSize: 16px;color: #40a9ff;"
          />
          <!-- <a-divider type="vertical" />
          <a-popconfirm
            v-if="tableData.length"
            title="确定删除吗?"
            @confirm="() => handleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm> -->
        </template>
      </a-table>

      <PlanModal
        ref="modalForm"
        :disabled="disabled"
        @ok="modalFormOk"
      ></PlanModal>

      <Procedure ref="Procedure" />

      <WorkOrderModal ref="WorkOrder" />

      <AutoMaticModal @ok="modalFormOk" ref="AutoMatic" />

      <!-- 二维码 -->
      <qrCode
        ref="qrCode"
        :label="qrModel.label"
        :text="qrModel.text"
        :qrSize="320"
      ></qrCode>
    </a-card>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import PlanModal from "@/components/PlanModal.vue";
import Procedure from "./components/Procedure";
import WorkOrderModal from "./components/WorkOrderModal.vue";
import AutoMaticModal from "./components/AutoMaticModal.vue";
import qrCode from "@/components/qrCode";
import api from "@/api/apiServer";
import { CodeNo } from "@/unit/QRCodeConfig";
export default {
  mixins: [tableMinxins],
  components: { PlanModal, Procedure, WorkOrderModal, AutoMaticModal, qrCode },
  data() {
    return {
      columns,
      tableData: [],
      treeData: [],
      Option: [],
      model: {},
      disabled: true,
      qrModel: {
        label: "", // 二维码底部内容
        text: "", // 二维码内容
      },
    };
  },
  mounted() {
    this.queryParam.planId = Number(this.$route.query.id);
    this.getList();
    this.getTree();
  },
  methods: {
    // 获取列表数据
    async getList(arg) {
      this.loading = true;
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.ProductPlanFindSon(params);
      this.tableData = res.data;
      this.loading = false;
    },
    handleCode(record) {
      let { planNum, id, goodsId } = record;
      let newObj = { id, planNum, goodsId, QRCode: CodeNo.ProductionPlan };
      let text = JSON.stringify(newObj);
      this.$refs.qrCode.title = "生产计划二维码";
      this.qrModel.label = `${planNum} `;
      this.qrModel.text = text;
      this.$refs.qrCode.list = this.qrModel.label.split(",");
      this.$refs.qrCode.showModal();
    },
    async handleStatus(record) {
      let params = Object.assign(record, {});
      if (record.status === 0) {
        params.status = 1;
      } else {
        params.status = 0;
      }
      const res = await api.ProductPlanUpdate(params);
      if (res.code && res.code !== 200) {
        this.$message.error("修改失败");
        this.confirmLoading = false;
        return;
      }
      this.$message.success("修改成功");
      this.getList();
    },
    modalFormOk() {
      this.queryParam.id = this.$route.query.id;
      this.getList();
    },
    //  编辑
    handleEdit(record) {
      this.$refs.modalForm.title = "编辑子生产计划";
      this.$refs.modalForm.modalEdit(record);
      this.getOption();
    },
    handleWork(record) {
      this.$refs.Procedure.showDrawer(record.bomId);
    },
    // 自动生成加工单
    handleWorkAutoMatic(record) {
      let { id, planNum, planQuantity, bomId, goodsId } = record;
      this.$refs.AutoMatic.title = "自动生成加工单";
      this.$refs.AutoMatic.modalEdit({
        id,
        planNum,
        planQuantity,
        bomId,
        goodsId,
      });
    },
    // 工单详情
    handleWorkOrder(record) {
      this.$refs.WorkOrder.showDrawer(record.id);
    },
    // 获取所有设备数据
    async getOption() {
      let params = { pageNumber: 1, pageSize: 999999, deleteFlag: "N" };
      const res = await api.CommodityList(params);
      this.$refs.modalForm.tableOption = res.data.data;
    },
    handleToChild(path, record) {
      let { id } = record;
      this.$router.push({ path, query: { id } });
    },
    // 删除
    async handleDelete(record) {
      let params = {
        id: record.id,
      };
      const res = await api.ProductDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
    async getTree() {
      const res = await api.CommodityTypeFindAll();
      this.treeData = res.data;
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
</style>
